<?php 
namespace Home\Controller;

use \Think\Controller;

class DengluController extends Controller
{

    //执行登陆
    public function dologin()
    {
        //接收用户名和密码
        $username = I('post.username');
        $password = I('post.password');

        //验证
        $user = M('user');
         $data = $user->where(array('username'=>$username))->find();
         if (!$data) {
             $this->error('用户不存在！');
             exit;
         }
         //验证密码
         if ($data['password'] != $password) {
             $this->error('密码不正确');
             exit;
         }
        
        //把用户信息添加到session
        $_SESSION['user']=$data;
        $list = M('node')->field('cname,aname')->where('id in '.M('roleposition-node')->field('nid')->where('rid in '. M('user')->field('uposition')->where(array('id'=>$_SESSION['user']['id']))->buildSql())->buildSql())->select();
        
        $nodelist = array();
        // $nodelist['Index'] = array('index');
         
        //遍历重新拼装
        foreach($list as $v){
            $nodelist[$v['cname']][] = $v['aname'];
            //把修改和执行修改 添加和执行添加 拼装到一起
            if($v['aname']=="edit"){
                $nodelist[$v['cname']][]="save";
            }
            if($v['aname']=="add"){
                $nodelist[$v['cname']][]="insert";
            }
            // 把doAjax也赋值给index拼装
            if($v['aname']=="index"){
                $nodelist[$v['cname']][]="doAjax";
                $nodelist[$v['cname']][]="upload";
                $nodelist[$v['cname']][]="mpass";
                $nodelist[$v['cname']][]="detail";
                $nodelist[$v['cname']][]="count";
            }
            if($v['aname']=="zwtiaozheng"){
                $nodelist[$v['cname']][]="edit";
            }
            if($v['aname']=="bmtiaozheng"){
                $nodelist[$v['cname']][]="bumen";
            }
            if($v['aname']=="lzgl"){
                $nodelist[$v['cname']][]="fire";
            }
            if($v['aname']=="tx"){
                $nodelist[$v['cname']][]="upload";
                $nodelist[$v['cname']][]="upload1";
            }

        }
       
        
        $_SESSION['user']['nodelist'] = $nodelist;

        
        //跳转到首页
        $this->redirect('Home/Yg/index');
    }

    //退出登陆
    public function logout()
    {
        $time1 = date('Y-m-d H:i',time());
        //echo $time1;exit;
        $uname = $_SESSION['user']['name'];
        M('userdetail')->where(array('uname'=>$uname))->setField('lastlogin',$time1);

        //清空session
        unset($_SESSION['user']);
        unset($_SESSION['weather']);

        //跳转
        $this->redirect('Index/index');
    }
}